//
//  BaseAlert.m
//  PostDelivery
//
//  Created by wmz on 2020/6/22.
//  Copyright © 2020 wmz. All rights reserved.
//

#import "BaseAlert.h"

@implementation BaseAlert

+ (void)alertTitle:(nullable NSString*)title message:(nullable NSString*)message confirm:(nullable NSString*)confirm cancel:(nullable NSString*)cancel listArr:(nullable NSArray*)arr block:(nullable AlertBlock)block{
    return [BaseAlert alertTitle:title message:message confirm:confirm cancel:cancel listArr:arr style:DialogTypeNornal block:block tag:0];
}

+ (void)alertTitle:(nullable NSString*)message confirm:(nullable NSString*)confirm block:(nullable AlertBlock)block{
    return [BaseAlert alertTitle:nil message:message confirm:confirm cancel:nil listArr:nil style:DialogTypeNornal block:block tag:0];
}

+ (void)alertTitle:(nullable NSString*)message block:(nullable AlertBlock)block{
     return [BaseAlert alertTitle:nil message:message confirm:@"确定" cancel:nil listArr:nil style:DialogTypeNornal block:block tag:0];
}

+ (void)alertTitle:(nullable NSString*)message cancel:(nullable NSString*)cancel block:(nullable AlertBlock)block{
     return [BaseAlert alertTitle:nil message:message confirm:@"确定" cancel:cancel listArr:nil style:DialogTypeNornal block:block tag:0];
}

+ (void)alertTitle:(nullable NSString*)message confirm:(nullable NSString*)confirm{
    return [BaseAlert alertTitle:nil message:message confirm:confirm cancel:nil listArr:nil style:DialogTypeNornal block:nil tag:0];
}

+ (void)alertTitle:(nullable NSString*)message{
    return [BaseAlert alertTitle:nil message:message confirm:@"确定" cancel:nil listArr:nil style:DialogTypeNornal block:nil tag:0];
}

+ (void)alertTitle:(nullable NSString*)message autoHideDurtion:(NSTimeInterval)durtion tag:(NSInteger)tag{
    return [BaseAlert alertTitle:nil message:message confirm:nil cancel:nil listArr:nil style:DialogTypeAuto block:nil tag:tag];
}

+ (void)alertTitle:(nullable NSString*)message tag:(NSInteger)tag{
    return [BaseAlert alertTitle:nil message:message confirm:@"确定" cancel:nil listArr:nil style:DialogTypeNornal block:nil tag:tag];
}

+ (void)alertTitle:(nullable NSString*)title message:(nullable NSString*)message confirm:(nullable NSString*)confirm cancel:(nullable NSString*)cancel listArr:(nullable NSArray*)arr style:(DialogType)style block:(nullable AlertBlock)block tag:(NSInteger)tag{
    
    WMZDialog *alert = Dialog();
    if (style == DialogTypeAuto) {
        alert.userInteractionEnabled = NO;
    }
    if (title) {
        alert.wTitleSet(title);
    }else{
        alert.wTitleSet(@"");
    }
    if (message) {
        alert.wMessageSet(message);
    }else{
        alert.wMessageSet(@"");
    }
    if (confirm) {
        alert.wOKColorSet(wColor_yellow)
        .wOKTitleSet(confirm)
        .wEventOKFinishSet(^(id anyID, id otherData) {
            if (block) {
                block(anyID,YES,NO);
            }
        });
    }
    if (cancel) {
        alert.wCancelTitleSet(cancel)
        .wEventCancelFinishSet(^(id anyID, id otherData) {
            if (block) {
                block(anyID,NO,YES);
            }
        });
    }
    if (arr&&[arr isKindOfClass:[NSArray class]]) {
        alert.wDataSet(arr)
        .wEventFinishSet(^(id anyID, NSIndexPath *path, DialogType type) {
            if (block) {
                block(anyID,NO,YES);
            }
        });
    }
    if (arr&&[arr isKindOfClass:[NSNumber class]]) {
        alert.wWirteKeyBoardTypeSet([(NSNumber*)arr intValue]);
    }
    alert
    .wMainOffsetYSet((style == DialogTypeAuto)?PostNum(20): PostNum(20))
    .wTagSet(tag?:random()%1000000)
    .wTypeSet(style)
    .wShadowCanTapSet(NO)
    .wStart();
    
}

+ (void)showHudSuccess:(nullable NSString*)text inView:(nullable UIView*)view{
    [BaseAlert showHudWithText:text inView:view style:LoadingStyleRight];
}

+ (void)showHudInfo:(nullable NSString*)text inView:(nullable UIView*)view{
    [BaseAlert showHudWithText:text inView:view style:LoadingStyleInfo];
}

+ (void)showHudError:(nullable NSString*)text inView:(nullable UIView*)view{
    [BaseAlert showHudWithText:text inView:view style:LoadingStyleError];
}

+ (void)showHudWithText:(nullable NSString*)text inView:(nullable UIView*)view style:(LoadingStyle)style{
    Dialog()
    .wLoadingColorSet(wColor_white)
    .wTitleSet(text)
    .wTypeSet(DialogTypeLoading)
    .wLoadingTypeSet(style)
    .wHideAnimationSet(AninatonHideNone)
    .wAnimationDurtionSet(0.3)
    .wShadowAlphaSet(0.01)
    .wDisappelSecondSet(1)
    .wLoadingSizeSet(CGSizeMake(50, 50))
    .wTagSet(1000000890)
    .wStartView(view?:[BaseAlert getCurrentVC].view);
}

+ (void)showHudWithText:(nullable NSString*)text inView:(nullable UIView*)view{
    WMZDialog*dialog =  Dialog()
    .wLoadingColorSet(DialogColor(0xFF9900))
    .wTitleSet(text)
    .wTypeSet(DialogTypeLoading)
    .wLoadingTypeSet(LoadingStyleSystem)
    .wShadowAlphaSet(0.01)
    .wShowCloseSet(NO)
    .wHideAnimationSet(AninatonHideNone)
    .wAnimationDurtionSet(1)
    .wTagSet(100000088);
    if (text && text.length) {
        dialog.wLoadingSizeSet(CGSizeMake(50, 50));
    }else{
        dialog.wLoadingSizeSet(CGSizeMake(30, 30));
    }
    dialog
    .wStartView(view?:[BaseAlert getCurrentVC].view);
}

+ (void)hideHudInView:(UIView*)view{
    [WMZDialog closeWithshowView:view?:[BaseAlert getCurrentVC].view tag:100000088 block:nil];
}

+ (void)showSelectTitle:(nullable NSString*)title dataSource:(NSArray*)dataSource block:(nullable PostSelectResult)block{
    [BaseAlert showSelectTitle:title defaultValue:nil dataSource:dataSource block:block];
}

+ (void)showSelectTitle:(nullable NSString*)title
           defaultValue:(nullable NSString*)value
             dataSource:(NSArray*)dataSource
                  block:(nullable PostSelectResult)block{
    Dialog()
    .wEventOKFinishSet(^(id anyID, id otherData) {
        if (block)
            block(anyID,otherData);
    })
    .wListDefaultValueSet(@[value?:@""])
    .wShowAnimationSet(AninatonShowTop)
    .wHideAnimationSet(AninatonHideTop)
    .wAnimationDurtionSet(0.15)
    .wTypeSet(DialogTypePickSelect)
    .wTitleSet(title)
    .wDataSet(dataSource)
    .wStart();
}

+ (UIViewController *)getCurrentVC{
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
    return currentVC;
}

+ (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC{
    UIViewController *currentVC;
    
    if ([rootVC presentedViewController]) {
        rootVC = [rootVC presentedViewController];
    }
    
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
        
    } else if ([rootVC isKindOfClass:[UINavigationController class]]){
        currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
        
    } else {
        currentVC = rootVC;
    }
    return currentVC;
}
@end
